package cn.test;

import com.baidu.aip.nlp.AipNlp;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;

public class AIPClient {

    private static AIPClient aip = null;
    private AipNlp client;

    private AIPClient(){
        this. client = new AipNlp(Config.APP_ID, Config.API_KEY, Config.SECRET_KEY);
        this.client.setConnectionTimeoutInMillis(2000);
        this.client.setSocketTimeoutInMillis(60000);
    }

    public static AIPClient getInstance(){
        if(aip==null){
            aip = new AIPClient();
        }
        return aip;
    }

    public JSONObject simnet(String text1, String text2) {
        return client.simnet(text1, text2, null);
    }

    public JSONObject simnet(String text1, String text2, HashMap<String, String> options){
        // 选择CNN算法
        //options.put("model", "CNN");
        return client.simnet(text1, text2, options);
    }


    public static void main(String[] args) throws JSONException {
        // 初始化一个AipNlp
        AIPClient aip = getInstance();

        // 获取两个文本的相似度
        JSONObject response = aip.simnet("28岁给西藏隆子县玉麦乡牧民卓嘎央宗姐妹回信，肯定他们父女两代接力为国守边的行为", "中共中央总书记国家主席中央军委主席习近平，28岁给西藏隆子县玉麦乡牧民卓嘎央宗姐妹回信");
        System.out.println(response.toString());

        // 选择CNN算法
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("model", "CNN");
        JSONObject response1 = aip.simnet("百度是个搜索公司", "谷歌是个搜索公司", options);
        System.out.println(response1.toString());
    }


}
